'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE } = app.Sequelize;

  const ExperienceComment = app.model.define('experience_comment', {
    comment_id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '评论编号',
    },
    experience_id: {
      type: INTEGER,
      allowNull: false,
      comment: '经验编号',
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: '用户编号',
    },
    parent_id: {
      type: INTEGER,
      allowNull: true,
      comment: '父评论ID（用于回复）',
    },
    comment_content: {
      type: TEXT,
      allowNull: false,
      comment: '评论内容',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      comment: '创建时间',
    },
  }, {
    tableName: 'experience_comment',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: false,
    comment: '经验评论表',
  });

  // 关联关系
  ExperienceComment.associate = function() {
    // 一个评论属于一个经验
    app.model.ExperienceComment.belongsTo(app.model.InterviewExperience, {
      foreignKey: 'experience_id',
      as: 'experience',
    });
    
    // 自关联：回复关系
    app.model.ExperienceComment.hasMany(app.model.ExperienceComment, {
      foreignKey: 'parent_id',
      as: 'replies',
    });
    
    app.model.ExperienceComment.belongsTo(app.model.ExperienceComment, {
      foreignKey: 'parent_id',
      as: 'parent',
    });
  };

  return ExperienceComment;
};
